package com.example.netty.chatSystem.handler;

import com.example.netty.chatSystem.channelService.ChannelService;
import com.example.netty.chatSystem.message.ReceiveMessage;
import com.example.netty.chatSystem.message.SendMessage;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
@ChannelHandler.Sharable
public class ChatReceiveHandler extends SimpleChannelInboundHandler<SendMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, SendMessage msg) throws Exception {
        System.out.println("------------");
        Channel channel = ChannelService.getHashMap().get(msg.to);
        System.out.println("============");
        System.out.println("111111"+channel);
        if (channel == null){
            ReceiveMessage receiveMessage = new ReceiveMessage("系统",msg.to+"不在线或者该用户不存在");
            ctx.writeAndFlush(receiveMessage);
        }else {
            ReceiveMessage receiveMessage = new ReceiveMessage(msg.from,msg.content);
            channel.writeAndFlush(receiveMessage);
        }
    }
}
